<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="/Public/js/three.min.js"></script>
    <script src="/Public/js/three_shaders.js"></script>
    <script src="/Public/js/OrbitControls.js"></script>
    <script src="/Public/js/jquery-3.7.1.min.js"></script>
    <script src="/Public/js/geotiff.js"></script>
    <style>
        body {
            margin: 0;
            overflow: hidden;
            background: #1a1a2e;
            font-family: Arial, sans-serif;
        }
    </style>

</head>

<body>
    <script>
        let terrainMesh = null;
        let heightScale = 0.5;
        let smoothness = 3;

        const scene = new THREE.Scene();
        scene.background = new THREE.Color(0x1a1a2e);

        // 创建相机
        const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 10000);
        camera.position.set(1, 50, 3);

        // 创建渲染器
        const renderer = new THREE.WebGLRenderer({ antialias: true });
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);

        // 添加光源
        const ambientLight = new THREE.AmbientLight(0x404040);
        scene.add(ambientLight);
        const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
        directionalLight.position.set(1, 1, 1);
        scene.add(directionalLight);


        const points = [
            new THREE.Vector3(-10, 0, 0),
            new THREE.Vector3(0, 0, 0),
        ];
        const lineGeometry = new THREE.BufferGeometry().setFromPoints(points);
        lineGeometry.computeBoundingBox();
        const boundingBox = lineGeometry.boundingBox;

        const vertexShader = `
            uniform vec3 uBoundingBoxMin;
            uniform vec3 uBoundingBoxMax;
            varying float vT;
            void main() {
                vT = (position.x - uBoundingBoxMin.x) / (uBoundingBoxMax.x - uBoundingBoxMin.x);
                gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
            }
        `;

        const fragmentShader = `
            uniform float pl_len;
            varying float vT;
            void main() {
                float segment = mod(vT * 20.0, 1.0);
                if(segment < 0.4) {
                    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
                } else {
                    discard;
                }
            }
        `;

        const lineMaterial = new THREE.ShaderMaterial({
            vertexShader: vertexShader,
            fragmentShader: fragmentShader,
            uniforms: {
                uBoundingBoxMin: { value: boundingBox.min },
                uBoundingBoxMax: { value: boundingBox.max }
            },
            transparent: true,
        });

        const line = new THREE.Line(lineGeometry, lineMaterial);
        scene.add(line);
        // 添加控制器
        const controls = new THREE.OrbitControls(camera, renderer.domElement);
        controls.enableDamping = true;

        // 动画循环
        function animate() {
            requestAnimationFrame(animate);
            // lineMaterial.uniforms.time.value += 0.001;
            controls.update();
            renderer.render(scene, camera);
        }
        animate();


    </script>
</body>

</html>